/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.api.mgrInterface.resourceInfo;

import com.inspur.edp.bef.api.be.BefLockType;
import com.inspur.edp.bef.bizentity.GspBusinessEntity;
import com.inspur.edp.bef.bizentity.beenum.GspDataLockType;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.utils.ExpressionUtils;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.apiGenerator.CefApiGeneratorContext;
import com.inspur.edp.cef.generator.apiGenerator.dataType.mgrInterface.resourceInfo.ModelInfoCtorGenerator;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.SuperConstructorInvocation;

public class BefModelInfoConstructorGenerator extends ModelInfoCtorGenerator {

  private final String className;
  private final String currentSu;
  private final String resMetadataId;
  private GspBusinessEntity be;
  private final String rootNodeCode;
  private final String displayKey;
  private CefApiGeneratorContext parentContext;
  private final GspDataLockType lockType;

  protected BefModelInfoConstructorGenerator(String className, String currentSu, String resMetadataId,
      GspBusinessEntity be, String rootNodeCode, String displayKey,
      CefApiGeneratorContext parentContext, GspDataLockType lockType) {
    super(className, currentSu, resMetadataId, rootNodeCode, displayKey, parentContext);
    this.className = className;
    this.currentSu = currentSu;
    this.resMetadataId = resMetadataId;
    this.be = be;
    this.rootNodeCode = rootNodeCode;
    this.displayKey = displayKey;
    this.parentContext = parentContext;
    this.lockType = lockType;
  }

  @Override
  protected Block buildMethodBody() {
    Block block = this.ast.newBlock();
    addSuperCtorInvocation(block);//super(xxx, xxx)
    addSetRootEntityStatement(block);//setRootEntityResInfo(new xxx)
    addSetTccStatement(block);//setTccSupported(xx); setTccAutoLock(xxx); ...
    return block;
  }

  private static boolean defaultValue_AutoTccLock = true;
  private static boolean defaultValue_AutoTccConfirm = true;
  private static boolean defaultValue_AutoTccCancel = true;
  private void addSetTccStatement(Block block) {
    if(!be.isTccSupported())
      return;

    MethodInvocation setTccSupportedInvocation = ast.newMethodInvocation();
    setTccSupportedInvocation.setName(ast.newSimpleName("setTccSupported"));
    setTccSupportedInvocation.arguments().add(ast.newBooleanLiteral(be.isTccSupported()));
    block.statements().add(ast.newExpressionStatement(setTccSupportedInvocation));

    if(be.isAutoTccLock() != defaultValue_AutoTccLock) {
      MethodInvocation methodInvocation = ast.newMethodInvocation();
      methodInvocation.setName(ast.newSimpleName("setAutoTccLock"));
      methodInvocation.arguments().add(ast.newBooleanLiteral(be.isAutoTccLock()));
      block.statements().add(ast.newExpressionStatement(methodInvocation));
    }

    if(be.isAutoCancel() != defaultValue_AutoTccConfirm) {
      MethodInvocation methodInvocation = ast.newMethodInvocation();
      methodInvocation.setName(ast.newSimpleName("setAutoCancel"));
      methodInvocation.arguments().add(ast.newBooleanLiteral(be.isAutoCancel()));
      block.statements().add(ast.newExpressionStatement(methodInvocation));
    }

    if(be.isAutoComplete() != defaultValue_AutoTccCancel) {
      MethodInvocation methodInvocation = ast.newMethodInvocation();
      methodInvocation.setName(ast.newSimpleName("setAutoConfirm"));
      methodInvocation.arguments().add(ast.newBooleanLiteral(be.isAutoComplete()));
      block.statements().add(ast.newExpressionStatement(methodInvocation));
    }
  }

  private void addSetRootEntityStatement(Block block) {
    MediateType entityType = this.parentContext.getCefGenContext()
        .getEntityResourceInfos().get(this.rootNodeCode);
    String typeName = entityType.getFullName();
    TypeInfo type = new TypeInfo(typeName);
    type.setUseFullName(true);
    MethodInvocation invocation = this.ast.newMethodInvocation();
    invocation.setName(this.ast.newSimpleName("setRootEntityResInfo"));
    ClassInstanceCreation creation = this.ast.newClassInstanceCreation();
    creation.setType(type.getType(this.ast, this.parentContext));
    creation.arguments().add(this.ast.newThisExpression());
    invocation.arguments().add(creation);
    block.statements().add(this.ast.newExpressionStatement(invocation));
  }

  private void addSuperCtorInvocation(Block block) {
    SuperConstructorInvocation superConstructorInvocation = this.ast
        .newSuperConstructorInvocation();
    superConstructorInvocation.arguments()
        .add(ExpressionUtils.getStringLiteral(this.ast, this.currentSu));
    superConstructorInvocation.arguments()
        .add(ExpressionUtils.getStringLiteral(this.ast, this.resMetadataId));
    superConstructorInvocation.arguments().add(ExpressionUtils.getStringLiteral(ast,be.getID()));
    superConstructorInvocation.arguments()
        .add(ExpressionUtils.getStringLiteral(this.ast, this.displayKey));
    if (lockType == GspDataLockType.None) {
      addImport(BefLockType.class.getTypeName());
      superConstructorInvocation.arguments().add(ast.newName("BefLockType.None"));
    }
    block.statements().add(superConstructorInvocation);
  }
}
